const User = require('../../db/userSchema');
module.exports = async(req, res) => {
    console.log(req.body);
    // res.send('请求登录');
    // 请求数据库操作时,先请求出数据然后判断是否匹配做其余操作
    let userInfo = {
        username: req.body.username,
        password: req.body.password
    };
    // 通过用户名和数据库进行匹配
    let usernamerel = await User.findOne({ "username": req.body.username });
    // 如果用户名不存在
    if (!usernamerel) {
        res.send('<script>alert("用户名不存在");location.href = "/admin/login";</script>');
    } else {
        let inforel = await User.findOne(userInfo);
        if (inforel) {
            // 设置ejs的公共数据
            console.log(req.app.locals);
            req.app.locals.username = req.body.username;
            req.session.username = req.body.username;
            res.render('./admin/index.ejs');
        } else {
            res.send('<script>alert("密码错了,请重新输入");location.href = "/admin/login";</script>');
        }
    }


}